﻿using System.Collections.Generic;

namespace System.Linq.Functional
{
    public static class FoldExtensions
    {
        // Seq.fold
        public static T Fold<T, U>(this IEnumerable<U> items, Func<T, U, T> func, T acc)
        {
            foreach (var item in items)
                acc = func(acc, item);

            return acc;
        }

        // F# List.fold_left
        public static T FoldLeft<T, U>(this IList<U> list, Func<T, U, T> func, T acc)
        {
            for (var index = 0; index < list.Count; index++)
                acc = func(acc, list[index]);

            return acc;
        }

        // F# List.fold_right
        public static T FoldRight<T, U>(this IList<U> list, Func<T, U, T> func, T acc)
        {
            for (var index = list.Count - 1; index >= 0; index--)
                acc = func(acc, list[index]);

            return acc;
        }
    }
}
